package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 往购物车添加菜品和套餐
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //查询购物车当前是否有菜品
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .one();
        //判断是否有菜品或套餐
        if (cart != null) {
            //如果存在,就在原有的数量上加1
            cart.setNumber(cart.getNumber() + 1);
            //调用修改方法,修改数量
            shoppingCartService.updateById(cart);
            //响应购物车里的数据
            return R.success(cart);
        } else {
            //如果不存在,就添加
            shoppingCart.setUserId(BaseContext.getCurrentId());
            //设置数量为当前添加的数量
            shoppingCart.setNumber(shoppingCart.getNumber());
            //设置添加时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //调用添加方法,添加数据
            shoppingCartService.save(shoppingCart);
            //响应购物车里的数据
            return R.success(shoppingCart);
        }
    }

    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> select(){
        //获取线程中的用户id
        Long UserId = BaseContext.getCurrentId();
        //调用条件查询方法
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                //判断用户id相等,根据用户id查询购物车
                .eq(ShoppingCart::getUserId,UserId)
                //查询所有
                .list();
        //响应查询到的购物车信息
       return R.success(list);
    }

    /**
     * 删除购物车的套餐或菜品数量
     */
    @PostMapping("sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //获取当前线程的用户id
        Long UserId = BaseContext.getCurrentId();
        //调用条件查询方法
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                //判断用户id是否相等 (根据用户id查询)
                .eq(ShoppingCart::getUserId, UserId)
                //判断是否有菜品id,根据菜品id查询
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                //判断是否有套餐id,根据套餐id查询
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                //查询一个
                .one();
        //判断查询的菜品或套餐数量是否大于1
        if (cart.getNumber()>1){
            //如果大于1就设置数量为当前数量减1
            cart.setNumber(cart.getNumber()-1);
            //调用根据id修改方法
            shoppingCartService.updateById(cart);
            //将修改后的购物车信息响应回去
            return R.success(cart);
        }
            shoppingCart.setNumber(0);
            //如果数量等于1或小于1,调用根据id删除方法删除当前菜品或套餐
            shoppingCartService.removeById(cart.getId());
            //响应修改后的购物车信息
            return R.success(shoppingCart);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //获取当前线程id
        Long UserId = BaseContext.getCurrentId();
        //调用条件修改方法
        shoppingCartService.lambdaUpdate()
                //根据用户id修改
                .eq(ShoppingCart::getUserId,UserId)
                //删除所有
                .remove();
        //响应删除成功
        return R.success("删除成功");
    }

}
